﻿<?php
	/**
	 *	Google 短网址的API
	 *  详细介绍查看Google API文档：http://code.google.com/apis/urlshortener/overview.html
	 * 	程序代码来自标点符：http://www.biaodianfu.com/php-google-urlshortener-api.html
	*/
	/**
	 *	生成Google 短网址的函数
	 *	
	*/
	function shortenGoogleUrl($long_url){
		$apiKey = 'API-KEY'; //Get API key from : http://code.google.com/apis/console/
		$postData = array('longUrl' => $long_url, 'key' => $apiKey);
		$jsonData = json_encode($postData);
		$curlObj = curl_init();
		curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
		curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curlObj, CURLOPT_HEADER, 0);
		curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
		curl_setopt($curlObj, CURLOPT_POST, 1);
		curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
		$response = curl_exec($curlObj);
		curl_close($curlObj);
		$json = json_decode($response);
		return $json->id;
	}
	
	/**
	 *	还原Google短网址的函数
	*/
	function expandGoogleUrl($short_url){
		$curlObj = curl_init();
		curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?shortUrl='.$short_url);
		curl_setopt($curlObj, CURLOPT_HEADER, 0);
		curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
		$response = curl_exec($curlObj);
		curl_close($curlObj);
		$json = json_decode($response);
		return $json->longUrl;
	}
?>